#include "StdAfx.h"
#include "Picture.h"
#include "graphlibHTW.h"
#include "Constants.h"

Picture::Picture(void)
{

}

// operator overloading
Picture *Picture::operator=(Picture *_picture)
{
	htwDeleteImage(this->pic);	
	this->pic = (unsigned char *)htwCreateImage(FILENAME_PLACEHOLDER,_picture->pHeight,_picture->pWidth,_picture->pBytesPerPixel);
	this->pWidth = _picture->pWidth;
	this->pHeight = _picture->pHeight;
	this->pBytesPerPixel = _picture->pBytesPerPixel;

	return this;
}

// ctor
Picture::Picture(unsigned char *_pic, int _pWidth, int _pHeight, int _pBytesPerPixel)
{
	this->pic = _pic;
	this->pWidth = _pWidth;
	this->pHeight = _pHeight;
	this->pBytesPerPixel = _pBytesPerPixel;
}

// ctor
Picture::Picture(int _pWidth, int _pHeight, int _pBytesPerPixel)
{
	this->pic = (unsigned char *)htwCreateImage(FILENAME_PLACEHOLDER,_pHeight,_pWidth,_pBytesPerPixel);
	this->pWidth = _pWidth;
	this->pHeight = _pHeight;
	this->pBytesPerPixel = _pBytesPerPixel;
}

//dtor
Picture::~Picture(void)
{
	//htwDeleteImage(this->pic);	
}

// ctor
Picture::Picture(Picture *_picture)
{	
	this->pic = (unsigned char *)htwCreateImage(FILENAME_PLACEHOLDER,_picture->pHeight,_picture->pWidth,_picture->pBytesPerPixel);
	this->pWidth = _picture->pWidth;
	this->pHeight = _picture->pHeight;
	this->pBytesPerPixel = _picture->pBytesPerPixel;
}

unsigned char *Picture::getColorFromCoordinates(int _x, int _y)
{
	int offset = (_x + (_y * this->pWidth)) * 3;

	offset = offset - (offset % 3);
	unsigned char color[3];

	color[0] = this->pic[offset+2];
	color[1] = this->pic[offset+1];
	color[2] = this->pic[offset];
	
	return color;

}

void Picture::drawRectangle(int x0,int y0,int x1,int y1,int red,int green,int blue,int alpha)
{
	//cache picture width, set up offsets for drawing the rectangle
	int pictureWidth = this->pWidth;
	int start =  (x0 + y0 * pictureWidth) * 3;
	int end =  (x1 + y1 * pictureWidth) * 3;

	//almost invisible
	if (alpha == 0)
		alpha = 1;

	//for calculations (foreground = rectangle)
	unsigned char backgroundColorRed;
	unsigned char backgroundColorGreen;
	unsigned char backgroundColorBlue;
	unsigned char foregroundColorRed = red / (255 / alpha);
	unsigned char foregroundColorGreen = green / (255 / alpha);
	unsigned char foregroundColorBlue = blue / (255 / alpha);
	unsigned char computedAlpha = (alpha==255) ? 255 : (255 / (255 - alpha));

	//and the resulting colors
	unsigned char resultColorRed;
	unsigned char resultColorGreen;
	unsigned char resultColorBlue;

	for (int i = start; i <= end; i = i + 3) {
		int column = (i % (pictureWidth * 3)) / 3;
		int row = i / (pictureWidth * 3);

		//calculating resulting colors 
		if (column >= x0 && column <= x1 && row >= y0 && row <= y1)
		{
			backgroundColorBlue = this->pic[ i ];
			backgroundColorGreen = this->pic[ i + 1 ];
			backgroundColorRed = this->pic[ i + 2 ];
			resultColorBlue = (backgroundColorBlue / computedAlpha + foregroundColorBlue) > 255 ? 255 : (backgroundColorBlue / computedAlpha + foregroundColorBlue);
			resultColorGreen = (backgroundColorGreen / computedAlpha + foregroundColorGreen) > 255 ? 255 : (backgroundColorGreen / computedAlpha + foregroundColorGreen);
			resultColorRed = (backgroundColorRed / computedAlpha + foregroundColorRed) > 255 ? 255 : (backgroundColorRed / computedAlpha + foregroundColorRed);
			this->pic[ i ] = resultColorBlue;
			this->pic[ i + 1 ] = resultColorGreen;
			this->pic[ i + 2 ]= resultColorRed;
		}
	}

 
	

}